<template>
  <div>
    <h1>上传图片</h1>
    <!-- action: 设置图片上传接口地址，默认
      如果需要自定义上传的话，就要通过http-request属性覆盖action
      http-request：自定义上传图片的属性
      file-list：用于回显图片 -->
    <el-upload
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :http-request="successFn"
    >
      <i class="el-icon-plus" />
    </el-upload>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
// COS储存对象初始化
// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKIDvgBPPAm8ICpCMGtFK8etrYy6xX8TXKq5',
  SecretKey: 'ziAKDpBH6evvf8rSoukLhndDYeU5QWMw'
})
export default {
  data() {
    return {
      fileList: [{ url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242306111155-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660122253&t=89cd692d3246e390964e975e48dafc74' }]
    }
  },
  methods: {
    successFn(res) {
      // res是我们上传的对象
      console.log(26, res)
      cos.putObject({
        Bucket: 'hr-project-1312874549', /* 必须-储存桶名字 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: Date.now().toString(), /* 必须 上传的文件名*/
        StorageClass: 'STANDARD',
        Body: res.file, // 上传文件对象
        onProgress: function(progressData) {
          console.log(JSON.stringify(progressData))
        }
      }, function(err, data) {
        console.log(err || data)
      })
    }
  }
}
</script>

<style>

</style>
